package com.aurora.neo4j.dao;

import com.aurora.neo4j.result.PersonOneDepthRelationVo;
import com.aurora.spider.houyuantuan.Person;
import org.neo4j.ogm.model.Result;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface PersonRepository extends Neo4jRepository<Person,Long> {


    /**
     * 更新或者添加人物 仅添加name id
     * @param person
     * @return
     */
    @Query("MERGE(n:Person{id:$person.id}) SET n.name= $person.name ,n.id= $person.id,n.imgUrl= $person.imgUrl return n")
    Person simpleAddById(@Param("person") Map person);

    /**   /**
     * 更新或者添加人物 仅添加name
     * @param person
     * @return
     */
    @Query("MERGE(n:Person{name: $person.name}) SET n.name= $person.name,n.imgUrl= $person.imgUrl return n")
    Person simpleAddByName(@Param("person") Map person);
    /**
     * 通过name添加或者更新
     * @param person
     * @return
     */
    @Query("MERGE(n:Person{id: $person.id}) SET n.name= $person.name,n.id=$person.id,n.imgUrl=$person.imgUrl,n.info=$person.info return n")
    Person saveOrUpdate(@Param("person") Map person);


    /**
     * 关系展示名称，但执行异常 literal不存在
     * @param baseRelationShip
     * @param relation
     */
    @Query("MATCH (s:Person{id: $baseRelationShip.parentId}),(e:Person{id:$baseRelationShip.sonId}) MERGE (s)-[:`:#{literal(#relation)}`]->(e)")
    void createPersonRelation(@Param("baseRelationShip")Map baseRelationShip,@Param("relation")String relation);
    /**
     * 增加或者修改关系ByName
     * @param baseRelationShip
     */
    @Query("OPTIONAL MATCH  (s:Person{id: $baseRelationShip.parentId}),(e:Person{name:$baseRelationShip.sonName})" +
            "MERGE (s)-[:relation {name:$baseRelationShip.relation}]->(e)")
    void createPersonRelationByName(@Param("baseRelationShip")Map baseRelationShip);

    /**
     * 增加或者修改关系
     * @param baseRelationShip
     */
    @Query("OPTIONAL MATCH  (s:Person{id: $baseRelationShip.parentId}),(e:Person{id:$baseRelationShip.sonId})" +
            "MERGE (s)-[:relation {name:$baseRelationShip.relationName}]->(e)")
    void createPersonRelation(@Param("baseRelationShip")Map baseRelationShip);


    @Query("MATCH p= (n:Person{name:$name})-[*1..2]-() RETURN p limit $limit")
    Result findStarByName(@Param("name") String name, @Param("limit") int limit);

    @Query("MATCH  (n:Person{name:$name})-[f:relation]-(m) RETURN n.id as startId ,n.name as startName,f.name as relate,m.id as endId,m.name as endName")
    List<PersonOneDepthRelationVo> findStarOneDepthByName(@Param("name") String name);


//
//    @Query("MATCH p=(n:PersonNode)-[r:personRelationship*]->(m:PersonNode) " +
//            "WHERE 1=1 " +
//            "AND CASE WHEN size($dto.relationName) <> 0 THEN ALL(rel IN r WHERE rel.relationName IN $dto.relationName ) ELSE true END " +
//            "AND CASE WHEN size($dto.relationIdList) <> 0  THEN ALL(rel IN r WHERE rel.relationId IN $dto.relationIdList ) ELSE true END " +
//            "AND CASE WHEN size($dto.expertIdList) <> 0 THEN n.personId IN $dto.expertIdList ELSE true END " +
//            "AND CASE WHEN size($dto.auditPersonIdList) <> 0 THEN m. personId IN $dto.auditPersonIdList ELSE true END " +
//            "AND CASE WHEN $dto.other ='Y' THEN length(p) > 1 ELSE true END " +
//            "WITH n, m, relationships(p) AS rels " +
//            "UNWIND rels AS rs " +
//            "match (a:PersonNode{personId:startNode(rs).personId}),(b:PersonNode{personId:endNode(rs).personId}) "+
//            "WITH collect(distinct {personId:a.personId,type:a.type,name:a.name}) + collect(distinct {personId:b.personId,type:b.type,name:b.name}) as nodes1 "+
//            "UNWIND nodes1 as nodes " +
//            "RETURN DISTINCT nodes")
//    List getNode(@Param("dto") RelationConditionDto dto);



}
